package com.webapps.tvprogram.web;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.webapps.tvprogram.utils.GlobalInfo;

public class WebRetreiver {
    // private final String defaultXmlLocation =
    // "http://twim.eu/mobile/pasu_tvprogram_utf8.xml";
    private final String defaultXmlLocation = "http://twim.eu/mobile/rafalos/prog.xml";

    public Bitmap getBitmapFromLink(String stationName, String link) {
        return downloadImage(link);
    }

    private Bitmap downloadImage(String URL) {
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in = openHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    private InputStream openHttpConnection(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);
        URLConnection conn = url.openConnection();

        if (!(conn instanceof HttpURLConnection))
            throw new IOException("Not an HTTP connection");

        try {
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();

            response = httpConn.getResponseCode();
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();
            }
        } catch (Exception ex) {
            throw new IOException("Error connecting");
        }
        return in;
    }

    public void retreiveXmlFile() throws IOException {
        retreiveXmlFile(defaultXmlLocation);
    }

    public void retreiveXmlFile(String remotePathToXmlFile) throws IOException {
        String encoding = "UTF8";
        File dir = new File(GlobalInfo.PATH_TO_XML_FILE);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File f = new File(GlobalInfo.PATH_TO_XML_FILE + GlobalInfo.XML_FILE_NAME);
        if (!f.exists()) {
            f.createNewFile();
        }
        URL u = new URL(remotePathToXmlFile);
        HttpURLConnection c = (HttpURLConnection) u.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();
        FileOutputStream fos = new FileOutputStream(f);
        OutputStreamWriter osw = new OutputStreamWriter(fos, encoding);
        InputStream in = c.getInputStream();
        InputStreamReader insr = new InputStreamReader(in, encoding);
        char[] buffer = new char[1024];
        int len1 = 0;
        while ((len1 = insr.read(buffer)) > 0) {
            osw.write(buffer, 0, len1);
        }
        insr.close();
        osw.close();
    }
}
